using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using FlatRedBall.Audio;

using FlatRedBall.Instructions;
#endif


using Gnome.Engine;
using Gnome.Screens;
using FlatRedBall.Audio;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
namespace Gnome.Entities
{
    
	public partial class GnomePlayer
	{
        private static Dictionary<String, int> SpriteSceneColors = new Dictionary<string, int>()
        {
            {"Orange", 0},
            {"Purple", 1}
        };

        public static float MAX_SPEED = 10;
        private Obstacle.VariableState onTerrain;
        public bool isDead = false;
        public bool isPaused = false;
        public bool onHidingSpot = false;
        public bool isHiding = false;
        Sound mudslide = AudioManager.GetSound("mudslide");
        Xbox360GamePad gamepad;

        private HomeBase home;
        private HUD hud;

        PowerUp.VariableState powerup;

        Sprite mudsplash;
        Sprite overburdened;
        Sprite fastlines;

        public Obstacle.VariableState OnTerrain
        {
            get { return onTerrain; }
            set { onTerrain = value; }
        }

        public PowerUp.VariableState PowerUp
        {
            get { return powerup; }
            set { powerup = value; }
        }

        public HomeBase Home
        {
            get { return home; }
            set { home = value; }
        }

        public HUD HUD
        {
            get { return hud; }
            set { hud = value; }
        }

        public Xbox360GamePad Gamepad
        {
            get { return gamepad; }
        }

        public Sprite MudSplash
        {
            get { return mudsplash; }
        }

        public Sprite Overburdened
        {
            get { return overburdened; }
        }

        public Sprite DustCloud
        {
            get { return fastlines; }
        }

		private void CustomInitialize()
		{
            GnomeSprite = new Sprite();
            GnomeSprite.ScaleX = GnomeSpriteScene.Sprites[0].ScaleX;
            GnomeSprite.ScaleY = GnomeSpriteScene.Sprites[0].ScaleY;
            this.onTerrain = Obstacle.VariableState.None;
            this.moveSpeed = MAX_SPEED;

            isPaused = false;

            mudsplash = new Sprite();
            mudsplash.AnimationChains = MudScene.Sprites[0].AnimationChains;
            mudsplash.CurrentChainIndex = 0;
            mudsplash.Animate = true;
            SpriteManager.AddSprite(mudsplash);
            mudsplash.X = X;
            mudsplash.Y = Y - (GnomeSprite.ScaleY / 2);
            mudsplash.Z = Z;
            mudsplash.ScaleX = 1.2f;
            mudsplash.ScaleY = 0.8f;
            mudsplash.AttachTo(this, true);

            overburdened = new Sprite();
            overburdened.AnimationChains = swetscene.Sprites[0].AnimationChains;
            overburdened.CurrentChainIndex = 0;
            overburdened.Animate = true;
            overburdened.X = X;
            overburdened.Y = Y + (GnomeSprite.ScaleY);
            overburdened.Z = Z;
            overburdened.ScaleX = 1.2f;
            overburdened.ScaleY = 1.1f;
            overburdened.AttachTo(this, true);

            fastlines = new Sprite();
            fastlines.AnimationChains = DustScene.Sprites[0].AnimationChains;
            fastlines.CurrentChainIndex = 0;
            fastlines.Animate = true; ;
            SpriteManager.AddSprite(fastlines);
            fastlines.X = X;
            fastlines.Y = Y - (GnomeSprite.ScaleY / 2);
            fastlines.Z = Z;
            fastlines.ScaleX = 1.2f;
            fastlines.ScaleY = 0.8f;
            fastlines.AttachTo(this, true);
            fastlines.Visible = false;

            CollisionBox.ScaleY = CollisionBox.ScaleY / 2;
            CollisionBox.RelativeY = -(CollisionBox.ScaleY);

            //Making the sizes better:
            GnomeSprite.ScaleX = 0.75f;
            CollisionBox.ScaleX = 0.75f;

            powerup = Entities.PowerUp.VariableState.Normal;
		}

        public void setSprite(String color)
        {
            GnomeSprite = GnomeSpriteScene.Sprites[SpriteSceneColors[color]];
            GnomeSprite.AttachTo(this, false);
        }

        public void SetController()
        {
            //Set the gamepad if there is one.
            if (isSecondPlayer)
                gamepad = InputManager.Xbox360GamePads[1];
            else
                gamepad = InputManager.Xbox360GamePads[0];
        }

		private void CustomActivity()
		{
            if (overburdened.JustCycled)
            {
                overburdened.Animate = false;
                overburdened.Visible = false;
            }
            //If the gnome is dead, freeze all activity.
            if (isDead)
            {
                Velocity = new Vector3(0);
                return;
            }
            if (isPaused)
            {
                Velocity = new Vector3(0);
                return;
            }
            if (!isHiding)
            {
                bool up, down, left, right;
                bool isFast = powerup == Entities.PowerUp.VariableState.SpeedUp;

                up = CustomInputManager.isCharacterMoveUp(gamepad, !isSecondPlayer);
                down = CustomInputManager.isCharacterMoveDown(gamepad, !isSecondPlayer);
                left = CustomInputManager.isCharacterMoveLeft(gamepad, !isSecondPlayer);
                right = CustomInputManager.isCharacterMoveRight(gamepad, !isSecondPlayer);

                if (OnTerrain == Obstacle.VariableState.Mud)
                {
                    fastlines.Visible = false;
                    if (XVelocity != 0 || YVelocity != 0)
                        mudsplash.Visible = true;
                    else
                        mudsplash.Visible = false;
                    if (!mudslide.IsPlaying)
                    {
                        mudslide.Play();
                    }
                    
                    if (up)
                    {
                        YAcceleration = moveAcceleration;
                        LastDirection = "Up";
                    }
                    else if (down)
                    {
                        YAcceleration = -moveAcceleration;
                        LastDirection = "Down";
                    }
                    else
                    {
                        YAcceleration = 0;
                        if (left)
                        {
                            XAcceleration = -moveAcceleration;
                            LastDirection = "Left";
                        }
                        else if (right)
                        {
                            XAcceleration = moveAcceleration;
                            LastDirection = "Right";
                        }
                        else
                        {
                            XAcceleration = 0;
                        }
                    }
                    if (XVelocity > moveSpeed)
                        XVelocity = moveSpeed;
                    if (XVelocity < -moveSpeed)
                        XVelocity = -moveSpeed;
                    if (YVelocity > moveSpeed)
                        YVelocity = moveSpeed;
                    if (YVelocity < -moveSpeed)
                        YVelocity = -moveSpeed;
                }
                else
                {
                    mudsplash.Visible = false;

                    fastlines.Visible = isFast;
                    /*
                     * Uncomment this once we have the speed sound available.
                     */
                    /*
                    if (!fastsound.IsPlaying)
                    {
                        fastsound.Play();
                    }
                    */
                    Velocity = new Vector3(0);
                    Acceleration = new Vector3(0);
                    if (up)
                    {
                        YVelocity = moveSpeed;
                        LastDirection = "Up";
                    }
                    else if (down)
                    {
                        YVelocity = -moveSpeed;
                        LastDirection = "Down";
                    }
                    else if (left)
                    {
                        XVelocity = -moveSpeed;
                        LastDirection = "Left";
                    }
                    else if (right)
                    {
                        XVelocity = moveSpeed;
                        LastDirection = "Right";
                    }
                    else if (!(up || down || right || left))
                    {
                        XVelocity = 0;
                        YVelocity = 0;
                    }
                }
                if (!(up || down || right || left))
                    GnomeSprite.CurrentChainName = LastDirection + "Standing" + BagSize;
                else
                {
                    GnomeSprite.CurrentChainName = LastDirection + "Walking" + BagSize;
                    fastlines.CurrentChainName = LastDirection;
                }
            }
            else
            {
                Velocity = new Vector3(0);
            }

            if (PowerUpTimer > 0)
                PowerUpTimer -= TimeManager.SecondDifference;
            else if (powerup != Entities.PowerUp.VariableState.Normal)
                powerup = Entities.PowerUp.VariableState.Normal;
		}

        /*
         * Returns the type of an item that should be dropped.
         * */
        public Item.VariableState DropItem()
        {
            return Item.VariableState.Common1;
        }

        /*
         * Drops the player's current bag, resetting their bag weight and value.
         * Returns the bag as a new item.
         * */
        public Item DropBag()
        {
            //Create the bag item
            Item bag = ItemFactory.get(Item.VariableState.Bag, this.Position, ContentManagerName);
            bag.itemvalue = BagValue;
            bag.itemweight = BagWeight;
            bag.Owner = this;

            //Setup its despawning behavior
            bag.shouldDespawn = true;
            bag.SetupDespawnTimer();

            BagValue = 0;
            BagWeight = 0;
            canPickup = false;
            return bag;
        }

		private void CustomDestroy()
		{


		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }
	}
}
